#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Unit Test for web structure xml parsing

@author Wang Qiang
"""

import unittest
import urllib2
from pyquery import PyQuery
from JQSelector.JQSelector import JQSelect, JQSelectPQ


class UnitTest(unittest.TestCase):
    """
    test for web structure xml parsing 
    """
    strXmlFile = open("./webs/news/163.xml", 'r')
    strXml = strXmlFile.read()
    strXmlFile.close()

    formatXmlFile = open("./formats/news/163.xml", 'r')
    formatXml = formatXmlFile.read()
    formatXmlFile.close()


    def testStructureXml(self):
        pqStrXml = PyQuery(self.strXml)
        self.assertEqual(pqStrXml('a').attr('name'), '163')
        self.assertEqual(JQSelectPQ(self.strXml, 'a > format')[0].text(),
                         'news-163')
        self.assertTrue(True)

    def testLinkRetreival(self):
        notations = JQSelectPQ(self.strXml, 'a > notation')
        mainLink = JQSelectPQ(self.strXml, 'a')[0].attr('href')
        self.assertEqual(mainLink, "http://news.163.com") 
        self.strHtml = urllib2.urlopen(mainLink).read()
        alist = []
        for pqNotation in notations:
            num = pqNotation.attr('num')
            strNotation = pqNotation.text()
            if not num:
                num = 20
            num = int(num)
            res = JQSelect(self.strHtml, strNotation)
            resNum = (len(res) > num) and num or len(res)
            alist += res[0:resNum]
        self.assertEqual(len(alist), 8)

    def testFormat(self):
        testUrl = 'http://sports.163.com/11/0913/08/7DQN4M9E00051CDG.html'
        self.strHtml = urllib2.urlopen(testUrl).read()
        contents = JQSelectPQ(self.formatXml, 'content')
        res = {}
        for pqContent in contents:
            type = pqContent.attr('type')
            attr = pqContent.attr('attr')
            composit = pqContent.attr('composit')
            selectStr = pqContent.text()
            if attr:
                res[type] = PyQuery(JQSelect(self.strHtml,
                                             selectStr)[0]).attr(attr)
            else:
                if composit:
                    paras = JQSelectPQ(self.strHtml, selectStr)
                    print len(paras)
                    res[type] = ""
                    for para in paras:
                        res[type] = res[type] + para.text() + '\n'
                else:
                    res[type] = JQSelectPQ(self.strHtml, selectStr)[0].text()
        print res['full']

if __name__ == '__main__':
    # Test all
    unittest.main()

